/*******************************************************************************
  * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
  * @file     digitalClock.c
  * @author   UartTFT Application Team
  * @version  V0.0.1
  * @date     2023-01-04
  * @brief    This file contains the functions prototypes for the UartTFT
  *           Application display firmware.
 ********************************************************************************/

#include "digitalClock.h"
#include "levetop.h"
#include "data.h"
#include "rtc.h"
#include "keyBoard.h"

#include "popupBox.h"
#include "transport.h"



#define RTC_Blank 14
uint8_t RTC_num = 0;
uint8_t RTC_flag = 0;
RTC_Info gRTC_Info[RTC_SIZE];

void show_RTC(void)
{
	if (RTC_flag == 1)
		Get_date_variable_Id_Regular_update(pic_id);

	if (RTC_num > 0 && RTC_flag == 1)
	{
		RTC_flag = 0;
		for (uint8_t i = 0; i < RTC_num; i++)
		{
				Display_RTC(&gRTC_Info[i], 1);
		}
	}

	if (RTC_num == 0  && RTC_flag == 1)
		RTC_flag = 0;
}

/***********************************************************************************
* Function Name : RTC_display
* Description   : Get digital clock info.
* Input         : - type: digital clock info sources
				  - buff: digital clock info
* Output        : None
* Return        : None
************************************************************************************/
void RTC_display(uint8_t type, uint8_t *buff)
{
	uint16_t p_add, p_len, pointer;
	uint16_t x, y, w, h; // Display box
	uint16_t simage, eimage;
	uint8_t mode = 0;
	uint16_t overlay;
	if (type == 0) // instruction information source is configuration file
	{
		x = buff[5] + (buff[6] << 8);
		y = buff[7] + (buff[8] << 8);
		simage = buff[9] + (buff[10] << 8);
		eimage = buff[11] + (buff[12] << 8);
		mode = buff[13];
		overlay = buff[14] + (buff[15] << 8);
	}
	else if (type == 1) // instruction information ource is variable space
	{
		pointer = (buff[2] + (buff[3] << 8)) * 2;
		x = var[pointer + 1] + (var[pointer + 2] << 8);
		y = var[pointer + 3] + (var[pointer + 4] << 8);
		simage = var[pointer + 5] + (var[pointer + 6] << 8);
		eimage = var[pointer + 7] + (var[pointer + 8] << 8);
		mode = var[pointer + 9];
		overlay = var[pointer + 10] + (var[pointer + 11] << 8);
	}

	gRTC_Info[RTC_num].x = x;
	gRTC_Info[RTC_num].y = y;
	gRTC_Info[RTC_num].ID_start = simage;
	gRTC_Info[RTC_num].mode = mode;
	gRTC_Info[RTC_num].overlay = overlay;

	if (Pop_up_flag == 1)
	{
		gRTC_Info[RTC_num].x += Pop_up_x;
		gRTC_Info[RTC_num].y += Pop_up_y;
	}

	Display_RTC(&gRTC_Info[RTC_num], 0);
	RTC_num++;
}
/***********************************************************************************
* Function Name : Display_RTC
* Description   : Show digital clock.
* Input         : - gRTC: digital clock info sources
				  - status: digital clock status
* Output        : None
* Return        : None
************************************************************************************/
uint8_t Display_RTC(RTC_Info *gRTC, uint8_t status)
{
	uint8_t buff[12] = {0};
	uint8_t flag = 0;
	uint8_t mode = gRTC->mode;
	int8_t num = 0;
	uint16_t i = 0, ID[11];
	uint16_t x_date = 0, x_time = 0, width = 0, high = 0;
	uint16_t id_start = gRTC->ID_start;
	uint16_t x = gRTC->x;
	uint16_t y = gRTC->y;
	uint16_t x_src, y_src, x_des, y_des, w_real, h_real, cnt;

	uint8_t state = 0;	//0: Aligned 1: Unaligned


	if (mode == 0 || mode == 8) // Year/month/day/hour/minute/second
	{
		/* Display date*/
		num = 0;
		if (calendar.w_year / 1000)
			num = 0; // The number of digits is different
		else
		{
			num--;
			if ((calendar.w_year / 100 % 10) == 0)
			{
				num--;
				if ((calendar.w_year / 10 % 10) == 0)
					num--;
			}
		}

		if (num >= 0)
			ID[num] = (calendar.w_year) / 1000;
		if (num + 1 >= 0)
			ID[num + 1] = (calendar.w_year) / 100 % 10;
		if (num + 2 >= 0)
			ID[num + 2] = (calendar.w_year) / 10 % 10;
		if (num + 3 >= 0)
			ID[num + 3] = (calendar.w_year) % 10;
		ID[num + 4] = 11;
		if (state == 0)
		{
			ID[num + 5] = (calendar.w_month) / 10 % 10;
		}
		else
		{
			if (((calendar.w_month) / 10 % 10) == 0)
				num--;
			else
				ID[num + 5] = (calendar.w_month) / 10 % 10;
		}
		ID[num + 6] = (calendar.w_month) % 10;
		ID[num + 7] = 12;

		if (state == 0)
		{
			ID[num + 8] = (calendar.w_date) / 10 % 10;
		}
		else
		{
			if (((calendar.w_date) / 10 % 10) == 0)
				num--;
			else
				ID[num + 8] = (calendar.w_date) / 10 % 10;
		}

		ID[num + 9] = (calendar.w_date) % 10;
		if (mode == 0)
			cnt = num + 9;
		else
		{
			ID[num + 10] = 13;
			cnt = num + 10;
		}

		for (i = 0; i <= cnt; i++)
		{
			if (id_start + ID[i] >= addr_index[11])
				continue; // ICON is available
			LT_ReadFlash_UI(buff, addr_index[10] + 12 * (id_start + ID[i]) + multi_language * 12 * addr_index[11], 12);
			pic_add = (buff[0]) + (buff[1] << 8) + (buff[2] << 16) + (buff[3] << 24);
			pic_w = buff[4] + (buff[5] << 8);
			pic_h = buff[6] + (buff[7] << 8);
			pic_len = buff[8] + (buff[9] << 8) + (buff[10] << 16);
			pic_flag = buff[11];

			// Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
//			if (i == 0)
//			{
//				high = pic_h;
//				flag = pic_flag;
//			}
			if (Screen_DIR == 0)
			{
				w_real = pic_w;
			}
			else if (Screen_DIR == 1)
			{
				w_real = pic_h;
			}
			else if (Screen_DIR == 2)
			{
				w_real = pic_w;
			}
			else if (Screen_DIR == 3)
			{
				w_real = pic_h;
			}
				x_date += w_real;
			}


		/*In the keyboard input interface*/
		if (DioKb_flag == 1)
		{
			if (!(x > gDio_Info[Dio_num].Xe || (x + x_date) < gDio_Info[Dio_num].Xs || y > gDio_Info[Dio_num].Ye || (y + high) < gDio_Info[Dio_num].Ys))
			{
				return 0;
			}
		}
		else if (AioKb_flag == 1)
		{
			if (!(x > gAio_Info[Aio_num].Xe || (x + x_date) < gAio_Info[Aio_num].Xs || y > gAio_Info[Aio_num].Ye || (y + high) < gAio_Info[Aio_num].Ys))
			{
				return 0;
			}
		}
		else if (GBKioKb_flag == 1)
		{
			if (!(x > gGBKio_Info[GBKio_num].Xe || (x + x_date) < gGBKio_Info[GBKio_num].Xs || y > gGBKio_Info[GBKio_num].Ye || (y + high) < gGBKio_Info[GBKio_num].Ys))
			{
				return 0;
			}
		}

		x_date = 0;

		for (i = 0; i <= cnt; i++)
		{
			if (id_start + ID[i] >= addr_index[11])
				continue; // ICON is available
			LT_ReadFlash_UI(buff, addr_index[10] + 12 * (id_start + ID[i]) + multi_language * 12 * addr_index[11], 12);
			pic_add = (buff[0]) + (buff[1] << 8) + (buff[2] << 16) + (buff[3] << 24);
			pic_w = buff[4] + (buff[5] << 8);
			pic_h = buff[6] + (buff[7] << 8);
			pic_len = buff[8] + (buff[9] << 8) + (buff[10] << 16);
			pic_flag = buff[11];

			// Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
//			if (i == 0)
//			{
//				high = pic_h;
//				flag = pic_flag;
//			}

			if (pic_len != 0 && pic_flag != 0xFF) // ICON is not empty
			{
				if (Screen_DIR == 0)
				{
					// Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					if (i == 0)
					{
						high = pic_h;
						flag = pic_flag;
					}

					x_src = x + x_date;
					y_src = y;
					w_real = pic_w;
				}
				else if (Screen_DIR == 1)
				{
					// Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					if (i == 0)
					{
						width = pic_w;
						flag =  pic_flag;
					}

					x_src = x - width;
					y_src = y + x_date;
					w_real = pic_h;

				}
				else if (Screen_DIR == 2)
				{
					// Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					if (i == 0)
					{
						high = pic_h;
						flag = pic_flag;
					}

					x_src = x - x_date - pic_w;
					y_src = y - pic_h;
					w_real = pic_w;
				}
				else if (Screen_DIR == 3)
				{
					// Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					if (i == 0)
					{
						width = pic_w;
						flag = pic_flag;
					}
					x_src = x;
					y_src = y - pic_h - x_date;
					w_real = pic_h;
				}
				if (flag == 0 || flag == 1 || flag == 2)
					LT_ShowPicture(x_src, y_src, pic_w, pic_h, pic_add, pic_len, flag);

				x_date += w_real;
			}
		}

		/* Display time*/
		num = 0;
		if(state == 0)
		{
			ID[num] = (calendar.hour) / 10 % 10;
		}
		else
		{
			if ((calendar.hour / 10 % 10) == 0)
				num--; // The number of digits is different
			else
				ID[num] = (calendar.hour) / 10 % 10;
		}
		ID[num + 1] = (calendar.hour) % 10;
		ID[num + 2] = 10;
		ID[num + 3] = (calendar.min) / 10 % 10;
		ID[num + 4] = (calendar.min) % 10;
		ID[num + 5] = 10;
		ID[num + 6] = (calendar.sec) / 10 % 10;
		ID[num + 7] = (calendar.sec) % 10;
		cnt = num + 7;

		if (Screen_DIR == 0)
			x = x + x_date + RTC_Blank; // Pixel interval between time and date
		else if (Screen_DIR == 1)
			y = y + x_date + RTC_Blank;
		else if (Screen_DIR == 2)
			x = x - x_date - RTC_Blank;
		else if (Screen_DIR == 3)
			y = y - x_date - RTC_Blank;

		// display
		for (i = 0; i <= cnt; i++)
		{
			if (id_start + ID[i] >= addr_index[11])
				continue; // ICON is available
			LT_ReadFlash_UI(buff, addr_index[10] + 12 * (id_start + ID[i]) + multi_language * 12 * addr_index[11], 12);

			pic_add = (buff[0]) + (buff[1] << 8) + (buff[2] << 16) + (buff[3] << 24);
			pic_w = buff[4] + (buff[5] << 8);
			pic_h = buff[6] + (buff[7] << 8);
			pic_len = buff[8] + (buff[9] << 8) + (buff[10] << 16);
			pic_flag = buff[11];

			if (pic_len != 0 && pic_flag != 0xFF) // ICON is not empty
			{
				if (Screen_DIR == 0)
				{
					if (i == 0) // // Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					{
						high = pic_h;
						flag = pic_flag;
					}
					x_src = x + x_time;
					y_src = y;
					w_real = pic_w;
				}
				else if (Screen_DIR == 1)
				{
					if (i == 0) // // Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					{
						width = pic_w;
						flag = pic_flag;
					}
					x_src = x - width;
					y_src = y + x_time;
					w_real = pic_h;
				}
				else if (Screen_DIR == 2)
				{
					if (i == 0) // // Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					{
						high = pic_h;
						flag = pic_flag;
					}

					x_src = x - x_time - pic_w;
					y_src = y - pic_h;
					w_real = pic_w;
				}
				else if (Screen_DIR == 3)
				{
					if (i == 0) // // Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					{
						width = pic_w;
						flag = pic_flag;
					}
					x_src = x;
					y_src = y - pic_h - x_time;
					w_real = pic_h;

				}
				if (flag == 0 || flag == 1 || flag == 2)
					LT_ShowPicture(x_src, y_src, pic_w, pic_h, pic_add, pic_len, flag);
				x_time += w_real;

			}
		}
	}
	else if ((mode >= 1 && mode <= 6) || (mode >= 9 && mode <= 11))
	{
		if (mode == 1 || mode == 9)
		{
			num = 0;

			if (num >= 0)
				ID[num] = (calendar.w_year) / 1000;
			if (num + 1 >= 0)
				ID[num + 1] = (calendar.w_year) / 100 % 10;
			if (num + 2 >= 0)
				ID[num + 2] = (calendar.w_year) / 10 % 10;
			if (num + 3 >= 0)
				ID[num + 3] = (calendar.w_year) % 10;

			ID[num + 4] = 11;

			if(state == 0)
			{
				ID[num + 5] = calendar.w_month / 10 % 10;
			}
			else
			{
				if ((calendar.w_month / 10 % 10) == 0)
					num--;
				else
					ID[num + 5] = calendar.w_month / 10 % 10;
			}

			ID[num + 6] = (calendar.w_month) % 10;
			ID[num + 7] = 12;

			if(state == 0)
			{
				ID[num + 8] = calendar.w_date / 10 % 10;
			}
			else
			{
				if ((calendar.w_date / 10 % 10) == 0)
					num--;
				else
					ID[num + 8] = calendar.w_date / 10 % 10;
			}

			ID[num + 9] = calendar.w_date % 10;

			if (mode == 1)
				cnt = num + 9;
			else
			{
				ID[num + 10] = 13;
				cnt = num + 10;
			}
		}
		else if (mode == 2 || mode == 10)
		{
			num = 0;
			if (num >= 0)
				ID[num] = calendar.w_year / 1000;
			if (num + 1 >= 0)
				ID[num + 1] = calendar.w_year / 100 % 10;
			if (num + 2 >= 0)
				ID[num + 2] = calendar.w_year / 10 % 10;
			if (num + 3 >= 0)
				ID[num + 3] = calendar.w_year % 10;

			ID[num + 4] = 11;

			if(state == 0)
			{
				ID[num + 5] = calendar.w_month / 10 % 10;
			}
			else
			{
				if ((calendar.w_month / 10 % 10) == 0)
					num--;
				else
					ID[num + 5] = calendar.w_month / 10 % 10;
			}

			ID[num + 6] = calendar.w_month % 10;

			if (mode == 2)
				cnt = num + 6;
			else
			{
				ID[num + 7] = 12;
				cnt = num + 7;
			}
		}
		else if (mode == 3 || mode == 11)
		{
			num = 0;
			if (state == 0)
			{
				ID[num] = calendar.w_month / 10 % 10;
			}
			else
			{
				if ((calendar.w_month / 10 % 10) == 0)
					num--;
				else
					ID[num] = calendar.w_month / 10 % 10;
			}

			ID[num + 1] = calendar.w_month % 10;
			ID[num + 2] = 12;

			if (state == 0)
			{
				ID[num + 3] = calendar.w_date / 10 % 10;
			}
			else
			{
				if ((calendar.w_date / 10 % 10) == 0)
					num--;
				else
					ID[num + 3] = calendar.w_date / 10 % 10;
			}

			ID[num + 4] = calendar.w_date % 10;

			if (mode == 3)
				cnt = num + 4;
			else
			{
				ID[num + 5] = 13;
				cnt = num + 5;
			}
		}
		else if (mode == 4)
		{
			num = 0;
			if (state == 0)
			{
				ID[num] = calendar.hour / 10 % 10;
			}
			else
			{
				if ((calendar.hour / 10 % 10) == 0)
					num--;
				else
					ID[num] = calendar.hour / 10 % 10;
			}
			ID[num + 1] = calendar.hour % 10;
			ID[num + 2] = 10;
			ID[num + 3] = calendar.min / 10 % 10;
			ID[num + 4] = calendar.min % 10;
			ID[num + 5] = 10;
			ID[num + 6] = calendar.sec / 10 % 10;
			ID[num + 7] = calendar.sec % 10;
			cnt = num + 7;
		}
		else if (mode == 5)
		{
			num = 0;
			if (state == 0)
			{
				ID[num] = calendar.hour / 10 % 10;
			}
			else
			{
				if ((calendar.hour / 10 % 10) == 0)
					num--;
				else
					ID[num] = calendar.hour / 10 % 10;
			}
			ID[num + 1] = calendar.hour % 10;
			ID[num + 2] = 10;
			ID[num + 3] = calendar.min / 10 % 10;
			ID[num + 4] = calendar.min % 10;
			cnt = num + 4;
		}
		else if (mode == 6)
		{
			num = 0;
			ID[num] = calendar.min / 10 % 10;
			ID[num + 1] = calendar.min % 10;
			ID[num + 2] = 10;
			ID[num + 3] = calendar.sec / 10 % 10;
			ID[num + 4] = calendar.sec % 10;
			cnt = num + 4;
		}


		for (i = 0; i <= cnt; i++)
		{
			if (id_start + ID[i] >= addr_index[11])
				continue;

			LT_ReadFlash_UI(buff, addr_index[10] + 12 * (id_start + ID[i]) + multi_language * 12 * addr_index[11], 12);
			pic_add = (buff[0]) + (buff[1] << 8) + (buff[2] << 16) + (buff[3] << 24);
			pic_w = buff[4] + (buff[5] << 8);
			pic_h = buff[6] + (buff[7] << 8);
			pic_len = buff[8] + (buff[9] << 8) + (buff[10] << 16);
			pic_flag = buff[11];

			if (i == 0)
			{
				high = pic_h;
				flag = pic_flag;
			}
			if (pic_len != 0 && pic_flag != 0xFF)
			{

				x_time += pic_w;
			}
		}

		if (DioKb_flag == 1)
		{
			if (!(x > gDio_Info[Dio_num].Xe || (x + x_time) < gDio_Info[Dio_num].Xs || y > gDio_Info[Dio_num].Ye || (y + high) < gDio_Info[Dio_num].Ys))
			{
				return 0;
			}
		}
		else if (AioKb_flag == 1)
		{
			if (!(x > gAio_Info[Aio_num].Xe || (x + x_time) < gAio_Info[Aio_num].Xs || y > gAio_Info[Aio_num].Ye || (y + high) < gAio_Info[Aio_num].Ys))
			{
				return 0;
			}
		}
		else if (GBKioKb_flag == 1)
		{
			if (!(x > gGBKio_Info[GBKio_num].Xe || (x + x_time) < gGBKio_Info[GBKio_num].Xs || y > gGBKio_Info[GBKio_num].Ye || (y + high) < gGBKio_Info[GBKio_num].Ys))
			{
				return 0;
			}
		}

		x_time = 0;

		for (i = 0; i <= cnt; i++)
		{
			if (id_start + ID[i] >= addr_index[11])
				continue;

			LT_ReadFlash_UI(buff, addr_index[10] + 12 * (id_start + ID[i]) + multi_language * 12 * addr_index[11], 12);
			pic_add = (buff[0]) + (buff[1] << 8) + (buff[2] << 16) + (buff[3] << 24);
			pic_w = buff[4] + (buff[5] << 8);
			pic_h = buff[6] + (buff[7] << 8);
			pic_len = buff[8] + (buff[9] << 8) + (buff[10] << 16);
			pic_flag = buff[11];


			if (pic_len != 0 && pic_flag != 0xFF)
			{
				if (Screen_DIR == 0)
				{
					if (i == 0) // // Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					{
						high = pic_h;
						flag = pic_flag;
					}
					x_src = x  + x_time;
					y_src = y;
					w_real = pic_w;
				}
				else if (Screen_DIR == 1)
				{
					if (i == 0) // // Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					{
						width = pic_w;
						flag = pic_flag;
					}
					x_src = x - width;
					y_src = y + x_time;
					w_real = pic_h;
				}
				else if (Screen_DIR == 2)
				{
					if (i == 0) // // Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					{
						high = pic_h;
						flag = pic_flag;
					}

					x_src = x - x_time - pic_w;
					y_src = y - pic_h;
					w_real = pic_w;
				}
				else if (Screen_DIR == 3)
				{
					if (i == 0) // // Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					{
						width = pic_w;
						flag = pic_flag;
					}
					x_src = x;
					y_src = y - pic_h- x_time;
					w_real = pic_h;
				}

				if (flag == 0 || flag == 1 || flag == 2)
					LT_ShowPicture(x_src, y_src, pic_w, pic_h, pic_add, pic_len, flag);
				x_time += w_real;
			}
		}

//		if (gRTC->Len_last > x_time && status)
//		{
//			LT268_dispaly_CutPicture(x + x_time, y,
//									 x + x_time, y, gRTC->Len_last - x_time, high,
//									 LCD_XSIZE_TFT, FullScreen_addr, FullScreen_color, FullScreen_flag);
//		}
//		gRTC->Len_last = x_time;
	}
	else if (mode == 7) // week
	{
		ID[0] = 14 + calendar.week;

		if (id_start + ID[0] < addr_index[11])
		{
			LT_ReadFlash_UI(buff, addr_index[10] + 12 * (id_start + ID[0]) + multi_language * 12 * addr_index[11], 12);

			pic_add = (buff[0]) + (buff[1] << 8) + (buff[2] << 16) + (buff[3] << 24);
			pic_w = buff[4] + (buff[5] << 8);
			pic_h = buff[6] + (buff[7] << 8);
			pic_len = buff[8] + (buff[9] << 8) + (buff[10] << 16);
			pic_flag = buff[11];

			if (DioKb_flag == 1)
			{
				if (!(x > gDio_Info[Dio_num].Xe || (x + pic_w) < gDio_Info[Dio_num].Xs || y > gDio_Info[Dio_num].Ye || (y + pic_h) < gDio_Info[Dio_num].Ys))
				{
					return 0;
				}
			}
			else if (AioKb_flag == 1)
			{
				if (!(x > gAio_Info[Aio_num].Xe || (x + pic_w) < gAio_Info[Aio_num].Xs || y > gAio_Info[Aio_num].Ye || (y + pic_h) < gAio_Info[Aio_num].Ys))
				{
					return 0;
				}
			}
			else if (GBKioKb_flag == 1)
			{
				if (!(x > gGBKio_Info[GBKio_num].Xe || (x + pic_w) < gGBKio_Info[GBKio_num].Xs || y > gGBKio_Info[GBKio_num].Ye || (y + pic_h) < gGBKio_Info[GBKio_num].Ys))
				{
					return 0;
				}
			}

			if (pic_len != 0 && pic_flag != 0xFF)
			{

				if (Screen_DIR == 0)
				{
					if (i == 0) // // Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					{
						high = pic_h;
						flag = pic_flag;
					}
					x_src = x;
					y_src = y;
					w_real = pic_w;
				}
				else if (Screen_DIR == 1)
				{
					if (i == 0) // // Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					{
						width = pic_w;
						flag = pic_flag;
					}
					x_src = x - width;
					y_src = y;
					w_real = pic_h;
				}
				else if (Screen_DIR == 2)
				{
					if (i == 0) // // Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					{
						high = pic_h;
						flag = pic_flag;
					}

					x_src = x  - pic_w;
					y_src = y - pic_h;
					w_real = pic_w;
				}
				else if (Screen_DIR == 3)
				{
					if (i == 0) // // Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					{
						width = pic_w;
						flag = pic_flag;
					}
					x_src = x;
					y_src = y - pic_h;
					w_real = pic_h;
				}
				if (pic_flag == 0 || pic_flag == 1 || pic_flag == 2)
					LT_ShowPicture(x_src, y_src, pic_w, pic_h, pic_add, pic_len, pic_flag);
			}
		}
	}
}




uint8_t Display_RTC_to_buff(uint8_t type, uint8_t *buff2,uint16_t overlay_x ,uint16_t overlay_y,uint16_t overlay_w)
{
	uint8_t buff[12] = {0};
	uint8_t flag = 0;
	uint8_t mode = 0;
	int8_t num = 0;
	uint16_t i = 0, ID[11];
	uint16_t x_date = 0, x_time = 0, width = 0, high = 0;
	uint16_t x_src, y_src, x_des, y_des, w_real, h_real, cnt;


	uint16_t p_add, p_len, pointer;
	uint16_t x, y, w, h; // Display box
	uint16_t simage, eimage,id_start;
	uint16_t overlay;


	uint8_t state = 0;	//0: Aligned 1: Unaligned



	if (type == 0) // instruction information source is configuration file
	{
		x = buff2[5] + (buff2[6] << 8);
		y = buff2[7] + (buff2[8] << 8);
		simage = buff2[9] + (buff2[10] << 8);
		eimage = buff2[11] + (buff2[12] << 8);
		mode = buff2[13];
		overlay = buff2[14] + (buff2[15] << 8);
	}
	else if (type == 1) // instruction information ource is variable space
	{
		pointer = (buff2[2] + (buff2[3] << 8)) * 2;
		x = var[pointer + 1] + (var[pointer + 2] << 8);
		y = var[pointer + 3] + (var[pointer + 4] << 8);
		simage = var[pointer + 5] + (var[pointer + 6] << 8);
		eimage = var[pointer + 7] + (var[pointer + 8] << 8);
		mode = var[pointer + 9];
		overlay = var[pointer + 10] + (var[pointer + 11] << 8);
	}

	id_start = simage;

	if (Pop_up_flag == 1)
	{
		x += Pop_up_x;
		y += Pop_up_y;
	}

	if(pic_update == 0)
	{
		x  = x - overlay_x;
		y  = y - overlay_y;
	}

	if (mode == 0 || mode == 8) // Year/month/day/hour/minute/second
	{
		/* Display date*/
		num = 0;
		if (calendar.w_year / 1000)
			num = 0; // The number of digits is different
		else
		{
			num--;
			if ((calendar.w_year / 100 % 10) == 0)
			{
				num--;
				if ((calendar.w_year / 10 % 10) == 0)
					num--;
			}
		}

		if (num >= 0)
			ID[num] = (calendar.w_year) / 1000;
		if (num + 1 >= 0)
			ID[num + 1] = (calendar.w_year) / 100 % 10;
		if (num + 2 >= 0)
			ID[num + 2] = (calendar.w_year) / 10 % 10;
		if (num + 3 >= 0)
			ID[num + 3] = (calendar.w_year) % 10;
		ID[num + 4] = 11;
		if (state == 0)
		{
			ID[num + 5] = (calendar.w_month) / 10 % 10;
		}
		else
		{
			if (((calendar.w_month) / 10 % 10) == 0)
				num--;
			else
				ID[num + 5] = (calendar.w_month) / 10 % 10;
		}
		ID[num + 6] = (calendar.w_month) % 10;
		ID[num + 7] = 12;

		if (state == 0)
		{
			ID[num + 8] = (calendar.w_date) / 10 % 10;
		}
		else
		{
			if (((calendar.w_date) / 10 % 10) == 0)
				num--;
			else
				ID[num + 8] = (calendar.w_date) / 10 % 10;
		}

		ID[num + 9] = (calendar.w_date) % 10;
		if (mode == 0)
			cnt = num + 9;
		else
		{
			ID[num + 10] = 13;
			cnt = num + 10;
		}

		for (i = 0; i <= cnt; i++)
		{
			if (id_start + ID[i] >= addr_index[11])
				continue; // ICON is available
			LT_ReadFlash_UI(buff, addr_index[10] + 12 * (id_start + ID[i]) + multi_language * 12 * addr_index[11], 12);
			pic_add = (buff[0]) + (buff[1] << 8) + (buff[2] << 16) + (buff[3] << 24);
			pic_w = buff[4] + (buff[5] << 8);
			pic_h = buff[6] + (buff[7] << 8);
			pic_len = buff[8] + (buff[9] << 8) + (buff[10] << 16);
			pic_flag = buff[11];

			// Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
//			if (i == 0)
//			{
//				high = pic_h;
//				flag = pic_flag;
//			}
			if (Screen_DIR == 0)
			{
				w_real = pic_w;
			}
			else if (Screen_DIR == 1)
			{
				w_real = pic_h;
			}
			else if (Screen_DIR == 2)
			{
				w_real = pic_w;
			}
			else if (Screen_DIR == 3)
			{
				w_real = pic_h;
			}
				x_date += w_real;
			}

		x_date = 0;

		for (i = 0; i <= cnt; i++)
		{
			if (id_start + ID[i] >= addr_index[11])
				continue; // ICON is available
			LT_ReadFlash_UI(buff, addr_index[10] + 12 * (id_start + ID[i]) + multi_language * 12 * addr_index[11], 12);
			pic_add = (buff[0]) + (buff[1] << 8) + (buff[2] << 16) + (buff[3] << 24);
			pic_w = buff[4] + (buff[5] << 8);
			pic_h = buff[6] + (buff[7] << 8);
			pic_len = buff[8] + (buff[9] << 8) + (buff[10] << 16);
			pic_flag = buff[11];

			// Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
//			if (i == 0)
//			{
//				high = pic_h;
//				flag = pic_flag;
//			}

			if (pic_len != 0 && pic_flag != 0xFF) // ICON is not empty
			{
				if (Screen_DIR == 0)
				{
					// Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					if (i == 0)
					{
						high = pic_h;
						flag = pic_flag;
					}

					x_src = x + x_date;
					y_src = y;
					w_real = pic_w;
				}
				else if (Screen_DIR == 1)
				{
					// Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					if (i == 0)
					{
						width = pic_w;
						flag =  pic_flag;
					}

					x_src = x - width;
					y_src = y + x_date;
					w_real = pic_h;

				}
				else if (Screen_DIR == 2)
				{
					// Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					if (i == 0)
					{
						high = pic_h;
						flag = pic_flag;
					}

					x_src = x - x_date - pic_w;
					y_src = y - pic_h;
					w_real = pic_w;
				}
				else if (Screen_DIR == 3)
				{
					// Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					if (i == 0)
					{
						width = pic_w;
						flag = pic_flag;
					}
					x_src = x;
					y_src = y - pic_h - x_date;
					w_real = pic_h;
				}
				if (flag == 0 || flag == 1 || flag == 2)
					//LT_ShowPicture(x_src, y_src, pic_w, pic_h, pic_add, pic_len, flag);

					LT268_pic_to_buffer(x_src, y_src, overlay_w, m_PngData,
										0, 0, pic_w, pic_add,
										pic_w, pic_h, flag);

				x_date += w_real;
			}
		}

		/* Display time*/
		num = 0;
		if(state == 0)
		{
			ID[num] = (calendar.hour) / 10 % 10;
		}
		else
		{
			if ((calendar.hour / 10 % 10) == 0)
				num--; // The number of digits is different
			else
				ID[num] = (calendar.hour) / 10 % 10;
		}
		ID[num + 1] = (calendar.hour) % 10;
		ID[num + 2] = 10;
		ID[num + 3] = (calendar.min) / 10 % 10;
		ID[num + 4] = (calendar.min) % 10;
		ID[num + 5] = 10;
		ID[num + 6] = (calendar.sec) / 10 % 10;
		ID[num + 7] = (calendar.sec) % 10;
		cnt = num + 7;

		if (Screen_DIR == 0)
			x = x + x_date + RTC_Blank; // Pixel interval between time and date
		else if (Screen_DIR == 1)
			y = y + x_date + RTC_Blank;
		else if (Screen_DIR == 2)
			x = x - x_date - RTC_Blank;
		else if (Screen_DIR == 3)
			y = y - x_date - RTC_Blank;

		// display
		for (i = 0; i <= cnt; i++)
		{
			if (id_start + ID[i] >= addr_index[11])
				continue; // ICON is available
			LT_ReadFlash_UI(buff, addr_index[10] + 12 * (id_start + ID[i]) + multi_language * 12 * addr_index[11], 12);

			pic_add = (buff[0]) + (buff[1] << 8) + (buff[2] << 16) + (buff[3] << 24);
			pic_w = buff[4] + (buff[5] << 8);
			pic_h = buff[6] + (buff[7] << 8);
			pic_len = buff[8] + (buff[9] << 8) + (buff[10] << 16);
			pic_flag = buff[11];

			if (pic_len != 0 && pic_flag != 0xFF) // ICON is not empty
			{
				if (Screen_DIR == 0)
				{
					if (i == 0) // // Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					{
						high = pic_h;
						flag = pic_flag;
					}
					x_src = x + x_time;
					y_src = y;
					w_real = pic_w;
				}
				else if (Screen_DIR == 1)
				{
					if (i == 0) // // Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					{
						width = pic_w;
						flag = pic_flag;
					}
					x_src = x - width;
					y_src = y + x_time;
					w_real = pic_h;
				}
				else if (Screen_DIR == 2)
				{
					if (i == 0) // // Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					{
						high = pic_h;
						flag = pic_flag;
					}

					x_src = x - x_time - pic_w;
					y_src = y - pic_h;
					w_real = pic_w;
				}
				else if (Screen_DIR == 3)
				{
					if (i == 0) // // Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					{
						width = pic_w;
						flag = pic_flag;
					}
					x_src = x;
					y_src = y - pic_h - x_time;
					w_real = pic_h;

				}
				if (flag == 0 || flag == 1 || flag == 2)
					LT268_pic_to_buffer(x_src, y_src, overlay_w, m_PngData,
										0, 0, pic_w, pic_add,
										pic_w, pic_h, flag);
				x_time += w_real;

			}
		}
	}
	else if ((mode >= 1 && mode <= 6) || (mode >= 9 && mode <= 11))
	{
		if (mode == 1 || mode == 9)
		{
			num = 0;

			if (num >= 0)
				ID[num] = (calendar.w_year) / 1000;
			if (num + 1 >= 0)
				ID[num + 1] = (calendar.w_year) / 100 % 10;
			if (num + 2 >= 0)
				ID[num + 2] = (calendar.w_year) / 10 % 10;
			if (num + 3 >= 0)
				ID[num + 3] = (calendar.w_year) % 10;

			ID[num + 4] = 11;

			if(state == 0)
			{
				ID[num + 5] = calendar.w_month / 10 % 10;
			}
			else
			{
				if ((calendar.w_month / 10 % 10) == 0)
					num--;
				else
					ID[num + 5] = calendar.w_month / 10 % 10;
			}

			ID[num + 6] = (calendar.w_month) % 10;
			ID[num + 7] = 12;

			if(state == 0)
			{
				ID[num + 8] = calendar.w_date / 10 % 10;
			}
			else
			{
				if ((calendar.w_date / 10 % 10) == 0)
					num--;
				else
					ID[num + 8] = calendar.w_date / 10 % 10;
			}

			ID[num + 9] = calendar.w_date % 10;

			if (mode == 1)
				cnt = num + 9;
			else
			{
				ID[num + 10] = 13;
				cnt = num + 10;
			}
		}
		else if (mode == 2 || mode == 10)
		{
			num = 0;
			if (num >= 0)
				ID[num] = calendar.w_year / 1000;
			if (num + 1 >= 0)
				ID[num + 1] = calendar.w_year / 100 % 10;
			if (num + 2 >= 0)
				ID[num + 2] = calendar.w_year / 10 % 10;
			if (num + 3 >= 0)
				ID[num + 3] = calendar.w_year % 10;

			ID[num + 4] = 11;

			if(state == 0)
			{
				ID[num + 5] = calendar.w_month / 10 % 10;
			}
			else
			{
				if ((calendar.w_month / 10 % 10) == 0)
					num--;
				else
					ID[num + 5] = calendar.w_month / 10 % 10;
			}

			ID[num + 6] = calendar.w_month % 10;

			if (mode == 2)
				cnt = num + 6;
			else
			{
				ID[num + 7] = 12;
				cnt = num + 7;
			}
		}
		else if (mode == 3 || mode == 11)
		{
			num = 0;
			if (state == 0)
			{
				ID[num] = calendar.w_month / 10 % 10;
			}
			else
			{
				if ((calendar.w_month / 10 % 10) == 0)
					num--;
				else
					ID[num] = calendar.w_month / 10 % 10;
			}

			ID[num + 1] = calendar.w_month % 10;
			ID[num + 2] = 12;

			if (state == 0)
			{
				ID[num + 3] = calendar.w_date / 10 % 10;
			}
			else
			{
				if ((calendar.w_date / 10 % 10) == 0)
					num--;
				else
					ID[num + 3] = calendar.w_date / 10 % 10;
			}

			ID[num + 4] = calendar.w_date % 10;

			if (mode == 3)
				cnt = num + 4;
			else
			{
				ID[num + 5] = 13;
				cnt = num + 5;
			}
		}
		else if (mode == 4)
		{
			num = 0;
			if (state == 0)
			{
				ID[num] = calendar.hour / 10 % 10;
			}
			else
			{
				if ((calendar.hour / 10 % 10) == 0)
					num--;
				else
					ID[num] = calendar.hour / 10 % 10;
			}
			ID[num + 1] = calendar.hour % 10;
			ID[num + 2] = 10;
			ID[num + 3] = calendar.min / 10 % 10;
			ID[num + 4] = calendar.min % 10;
			ID[num + 5] = 10;
			ID[num + 6] = calendar.sec / 10 % 10;
			ID[num + 7] = calendar.sec % 10;
			cnt = num + 7;
		}
		else if (mode == 5)
		{
			num = 0;
			if (state == 0)
			{
				ID[num] = calendar.hour / 10 % 10;
			}
			else
			{
				if ((calendar.hour / 10 % 10) == 0)
					num--;
				else
					ID[num] = calendar.hour / 10 % 10;
			}
			ID[num + 1] = calendar.hour % 10;
			ID[num + 2] = 10;
			ID[num + 3] = calendar.min / 10 % 10;
			ID[num + 4] = calendar.min % 10;
			cnt = num + 4;
		}
		else if (mode == 6)
		{
			num = 0;
			ID[num] = calendar.min / 10 % 10;
			ID[num + 1] = calendar.min % 10;
			ID[num + 2] = 10;
			ID[num + 3] = calendar.sec / 10 % 10;
			ID[num + 4] = calendar.sec % 10;
			cnt = num + 4;
		}


		for (i = 0; i <= cnt; i++)
		{
			if (id_start + ID[i] >= addr_index[11])
				continue;

			LT_ReadFlash_UI(buff, addr_index[10] + 12 * (id_start + ID[i]) + multi_language * 12 * addr_index[11], 12);
			pic_add = (buff[0]) + (buff[1] << 8) + (buff[2] << 16) + (buff[3] << 24);
			pic_w = buff[4] + (buff[5] << 8);
			pic_h = buff[6] + (buff[7] << 8);
			pic_len = buff[8] + (buff[9] << 8) + (buff[10] << 16);
			pic_flag = buff[11];

			if (i == 0)
			{
				high = pic_h;
				flag = pic_flag;
			}
			if (pic_len != 0 && pic_flag != 0xFF)
			{

				x_time += pic_w;
			}
		}


		x_time = 0;
		for (i = 0; i <= cnt; i++)
		{
			if (id_start + ID[i] >= addr_index[11])
				continue;

			LT_ReadFlash_UI(buff, addr_index[10] + 12 * (id_start + ID[i]) + multi_language * 12 * addr_index[11], 12);
			pic_add = (buff[0]) + (buff[1] << 8) + (buff[2] << 16) + (buff[3] << 24);
			pic_w = buff[4] + (buff[5] << 8);
			pic_h = buff[6] + (buff[7] << 8);
			pic_len = buff[8] + (buff[9] << 8) + (buff[10] << 16);
			pic_flag = buff[11];


			if (pic_len != 0 && pic_flag != 0xFF)
			{
				if (Screen_DIR == 0)
				{
					if (i == 0) // // Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					{
						high = pic_h;
						flag = pic_flag;
					}
					x_src = x  + x_time;
					y_src = y;
					w_real = pic_w;
				}
				else if (Screen_DIR == 1)
				{
					if (i == 0) // // Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					{
						width = pic_w;
						flag = pic_flag;
					}
					x_src = x - width;
					y_src = y + x_time;
					w_real = pic_h;
				}
				else if (Screen_DIR == 2)
				{
					if (i == 0) // // Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					{
						high = pic_h;
						flag = pic_flag;
					}

					x_src = x - x_time - pic_w;
					y_src = y - pic_h;
					w_real = pic_w;
				}
				else if (Screen_DIR == 3)
				{
					if (i == 0) // // Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					{
						width = pic_w;
						flag = pic_flag;
					}
					x_src = x;
					y_src = y - pic_h- x_time;
					w_real = pic_h;
				}

				if (flag == 0 || flag == 1 || flag == 2)
					LT268_pic_to_buffer(x_src, y_src, overlay_w, m_PngData,
															0, 0, pic_w, pic_add,
															pic_w, pic_h, pic_flag);
				x_time += w_real;
			}
		}

	}
	else if (mode == 7) // week
	{
		ID[0] = 14 + calendar.week;

		if (id_start + ID[0] < addr_index[11])
		{
			LT_ReadFlash_UI(buff, addr_index[10] + 12 * (id_start + ID[0]) + multi_language * 12 * addr_index[11], 12);

			pic_add = (buff[0]) + (buff[1] << 8) + (buff[2] << 16) + (buff[3] << 24);
			pic_w = buff[4] + (buff[5] << 8);
			pic_h = buff[6] + (buff[7] << 8);
			pic_len = buff[8] + (buff[9] << 8) + (buff[10] << 16);
			pic_flag = buff[11];



			if (pic_len != 0 && pic_flag != 0xFF)
			{

				if (Screen_DIR == 0)
				{
					if (i == 0) // // Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					{
						high = pic_h;
						flag = pic_flag;
					}
					x_src = x;
					y_src = y;
					w_real = pic_w;
				}
				else if (Screen_DIR == 1)
				{
					if (i == 0) // // Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					{
						width = pic_w;
						flag = pic_flag;
					}
					x_src = x - width;
					y_src = y;
					w_real = pic_h;
				}
				else if (Screen_DIR == 2)
				{
					if (i == 0) // // Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					{
						high = pic_h;
						flag = pic_flag;
					}

					x_src = x  - pic_w;
					y_src = y - pic_h;
					w_real = pic_w;
				}
				else if (Screen_DIR == 3)
				{
					if (i == 0) // // Record the height and flag of the first valid ICON, and the following ICON arrangement shall be subject to this
					{
						width = pic_w;
						flag = pic_flag;
					}
					x_src = x;
					y_src = y - pic_h;
					w_real = pic_h;
				}
				if (pic_flag == 0 || pic_flag == 1 || pic_flag == 2)
					LT268_pic_to_buffer(x_src, y_src, overlay_w, m_PngData,
										0, 0, pic_w, pic_add,
										pic_w, pic_h, flag);
			}
		}
	}
}


